package leetcode.offer;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * ClassName: FirstUniqueChar
 * Description:
 * date: 2021-11-01 18:39
 *
 * @author liyifan
 */
public class FirstUniqueChar {
    public char firstUniqChar(String s){
        if (s.length() == 1)
            return s.charAt(0);
        if (s.length() == 0)
            return ' ';
        LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();

        for (int i = 0;i < s.length();i++) {
            Character temp = s.charAt(i);
            if (map.containsKey(temp)) {
                int oldVal = map.get(temp);
                map.put(temp, oldVal + 1);
            } else {
                map.put(temp, 1);
            }
        }
        for (Map.Entry<Character, Integer> entry: map.entrySet()) {
            if (entry.getValue() == 1)
                return entry.getKey();
        }
        return ' ';
    }
}
